package com.liu.controller;

import com.liu.domain.text;
import com.liu.repository.TextRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by liu on 17-10-12.
 */
@RestController
public class TextController {
    private final static Logger logger = LoggerFactory.getLogger(TextController.class);

    @Autowired
    private TextRepository textRepository;

    /*
    查询所有寝室列表
     */
    @GetMapping(value = "/text")
    public List<text> textList() {
        System.out.println("textList");

        logger.info("textList");

        return textRepository.findAll();
        // return "{\"code\":0,\"msg\":\"\",\"count\":1000,\"data\":" + textRepository.findAll() + "}";

    }

    /*
    添加一个寝室
     */
    @PostMapping(value = "/text")
    public ModelAndView testAdd(text te) {
        textRepository.save(te);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("Addtext");
        mav.addObject("flag", 1);
        return mav;
    }

    /*
    更新一个寝室的信息
     */
    @PutMapping(value = "/text/{id}")
    public text studentUpdate(@PathVariable("id") int id, text te) {

        return textRepository.save(te);
    }
    /*
    删除一个寝室
     */

    @DeleteMapping(value = "/text/{id}")
    public boolean studentDelete(@PathVariable("id") int id) {
        textRepository.delete(id);
        return true;
    }

 //查询最新的公告
    @GetMapping(value = "/text/index")
    public text tex() {
        System.out.println("textList");

        logger.info("textList");

        return textRepository.findBytime().get(0);

    }
}
